/*
   Copyright (c) 2011, 2012, 2015 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *  2012-04-09 Marco Riccardi - modified structure of SymbolType 
 *  from ARTICLE:symbolType number("TOKEN") to UNKNOWN: symbolType number("TOKEN")
 *  2015-01-11 Marco Riccardi - added token2 
 *
 */
package org.mizartools.dli;

public class SymbolId {

	ArticleId articleId = null;
	SymbolType symbolType = null;
	String token1 = null;
	String token2 = null;
	int nr = -1;

	private SymbolId() {}

	public SymbolId(ArticleId articleId, SymbolType symbolType,	String token1,	String token2, int nr) throws DliException {
		if (articleId == null || symbolType == null || token1 == null || nr < 1){
			throw new DliException();
		}
		this.articleId = articleId;
		this.symbolType = symbolType;
		this.token1 = token1;
		this.token2 = token2;
		this.nr = nr;
	}
	
	public static SymbolId newInstance(String string) throws DliParseException{
		SymbolId symbolId = new SymbolId();
		int position = string.indexOf(":");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in SymbolId");
		}
		symbolId.articleId = ArticleId.newInstance(string.substring(0, position));
		string = string.substring(position+1, string.length());
		if (string.startsWith(" ")) string = string.substring(1);
		position = string.indexOf(" ");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in SymbolId");
		}
		try {
			symbolId.symbolType = SymbolType.valueOf(string.substring(0, position));
		}
		catch (Exception e) {
			throw new DliParseException("["+string+"] is not valid in SymbolId");
		}
		string = string.substring(position+1, string.length());
		position = string.indexOf("(\"");
		try {
			symbolId.nr = Integer.parseInt(string.substring(0, position));
		}
		catch(Exception e) {
			throw new DliParseException("["+string+"] is not valid in SymbolId");
		}
		symbolId.token1 = string.substring(position+2, string.length()-2);
		return symbolId;
	}
	
	@Override
	public String toString() {
		if (articleId.toString().equals("UNKNOWN")) {
			return "UNKNOWN: "+symbolType.toString()+" "+nr+"(\""+token1+"\")";
		} else {
			return articleId.toString()+":"+symbolType.toString()+" "+nr+"(\""+token1+"\")";
		}
	}

	public ArticleId getArticleId() {
		return articleId;
	}

	public SymbolType getSymbolType() {
		return symbolType;
	}

	public String getToken1() {
		return token1;
	}

	public String getToken2() {
		return token2;
	}

	public int getNr() {
		return nr;
	}

}
